package com.ycp.erp.main.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.imagchoose.ChooseManager;
import com.one.common.manager.imagchoose.IChooseImg;
import com.one.common.manager.imageloader.ILoader;
import com.one.common.manager.imageloader.LoaderManager;
import com.one.common.utils.StringUtils;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.widget.MyTitleBar;
import com.ycp.erp.R;
import com.ycp.erp.login.activity.LoginErpActivity;
import com.ycp.erp.view.HintDialog;
import com.ycp.erp.view.SingleWeelviewDialog;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * Class Des:个人资料
 * Created by bjh on 2021/9/3.
 */
@Route(path = RouterPath.PERSONAL_INFO)
public class PersonalInfoActivity extends BaseActivity {
    @BindView(R.id.ivAvatar)
    ImageView ivAvatar;
    @BindView(R.id.tvAccount)
    TextView tvAccount;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.rlAvatar)
    RelativeLayout rlAvatar;
    @BindView(R.id.rlName)
    RelativeLayout rlName;
    @BindView(R.id.tvPhone)
    TextView tvPhone;
    @BindView(R.id.rlPhone)
    RelativeLayout rlPhone;
    @BindView(R.id.tvEmail)
    TextView tvEmail;
    @BindView(R.id.rlEmail)
    RelativeLayout rlEmail;
    @BindView(R.id.tvSex)
    TextView tvSex;
    @BindView(R.id.rlSex)
    RelativeLayout rlSex;
    @BindView(R.id.rlPsw)
    RelativeLayout rlPsw;
    @BindView(R.id.tvLoginOut)
    TextView tvLoginOut;
    private ArrayList<String> values;
    @Override
    protected void initPresenter() {

    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        super.initView();
        RxView.clicks(tvLoginOut).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(o -> {
            //退出登录
            Intent loginActivityIntent = new Intent();
            loginActivityIntent.setClass(mContext, LoginErpActivity.class);
            loginActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(loginActivityIntent);
        });
        initSexData();
        RxView.clicks(rlPsw).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(o -> {
            RouterManager.getInstance().go(RouterPath.MODIFY_PSW);
        });
        RxView.clicks(rlName).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(o -> {
            HintDialog dialog = HintDialog.getInstance();
            dialog.setInputHint("请输入姓名");
            dialog.setTitle("设置姓名");
            dialog.setInputListener(new HintDialog.InputListener() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onConfirm(String data) {
                     tvName.setText(data);
                }
            });
            dialog.show(getSupportFragmentManager(),"");
        });
        RxView.clicks(rlPhone).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(o -> {
            HintDialog dialog = HintDialog.getInstance();
            dialog.setInputHint("请输入手机号");
            dialog.setTitle("设置手机号");
            dialog.setInputType(1);
            dialog.setInputListener(new HintDialog.InputListener() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onConfirm(String data) {
                    tvPhone.setText(data);
                }
            });
            dialog.show(getSupportFragmentManager(),"");
        });
        RxView.clicks(rlEmail).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(o -> {
            HintDialog dialog = HintDialog.getInstance();
            dialog.setInputHint("请输入邮箱");
            dialog.setTitle("设置邮箱");
            dialog.setInputType(2);
            dialog.setInputListener(new HintDialog.InputListener() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onConfirm(String data) {
                    tvEmail.setText(data);
                }
            });
            dialog.show(getSupportFragmentManager(),"");
        });
        RxView.clicks(rlSex).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(o -> {
            SingleWeelviewDialog singleWeelviewDialog =new SingleWeelviewDialog(getContext(), values, new SingleWeelviewDialog.WeelviewListener() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onItemSelected(int index) {
                   tvSex.setText(values.get(index));
                }
            });
            singleWeelviewDialog.show();
        });
        RxView.clicks(rlAvatar).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(o -> {
            ChooseManager.getChooser().chooseSingle(PersonalInfoActivity.this);
        });
    }
   private void  initSexData(){
       values=new ArrayList<>();
       values.add("男");
       values.add("女");

    }
    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("个人资料").hideLine();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //更换头像
            ArrayList<String> pathList =
                    data.getStringArrayListExtra(IChooseImg.EXTRA_RESULT_SELECTION);
            if (pathList != null && pathList.size() > 0) {
                String path = pathList.get(0);
                if (StringUtils.isNotBlank(path)) {
                    ILoader.Options options = ILoader.Options.circleOptions();
                    options.loadingResId = R.mipmap.ic_default_avatar;
                    options.loadErrorResId = R.mipmap.ic_default_avatar;
                    LoaderManager.getLoader().loadNet(ivAvatar, path,
                            options);
                }
            }
        }
    }
    @Override
    public int getLayoutResId() {
        return R.layout.activity_personal_info;
    }
}
